<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Like Model
 * @description : Database functions related to user likes
 * 
 */
class Like_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  // Insert new like data
  function add_like($like_type = NULL, $like_type_id = NULL, $user_id = NULL, $like_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($like_type) && !empty($like_type_id) && is_numeric($like_type_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_insr = array(
          'like_type' => $like_type,
          'like_type_id' => $like_type_id,
          'like_user_id' => $user_id,
          'like_status' => $like_status,
          'like_date' => date("Y-m-d H:i:s")
      );
      $query_sql = $this->db->insert_string('likes', $query_insr);
      $query_sql = str_replace('INSERT INTO', 'INSERT IGNORE INTO', $query_sql);
      $query_exec = $this->db->query($query_sql);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Remove (Unlike) like data
  function del_like($like_type = NULL, $like_type_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($like_type) && !empty($like_type_id) && is_numeric($like_type_id) && !empty($user_id) && is_numeric($user_id)) {
      $query_cond = array(
          'like_type' => $like_type,
          'like_type_id' => $like_type_id,
          'like_user_id' => $user_id
      );
      $query_exec = $this->db->delete('likes', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = 1;
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  // Get like data
  function get_like($like_type = NULL, $like_type_id = NULL, $like_user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($like_type) && !empty($like_type_id) && is_numeric($like_type_id)) {
      $query_cond = array(
          'like_type' => $like_type,
          'like_type_id' => $like_type_id,
          'user_status' => 1
      );
      if (!empty($like_user_id) && is_numeric($like_user_id)) {
        $query_cond['like_user_id'] = $like_user_id;
      }
      $this->db->join('users', 'user_id = like_user_id');
      $query_exec = $this->db->get_where('likes', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get users who likes
  function get_like_users($like_type = NULL, $like_type_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($like_type) && !empty($like_type_id) && is_numeric($like_type_id)) {
      $query_cond = array(
          'like_type' => $like_type,
          'like_type_id' => $like_type_id,
          'user_status' => 1
      );
      $this->db->join('users', 'user_id = like_user_id');
      $this->db->order_by('like_date', 'desc');
      $query_exec = $this->db->get_where('likes', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Get liked datas of a user
  function get_my_like($user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond['like_user_id'] = $user_id;
      $this->db->order_by('like_date', 'desc');
      $query_exec = $this->db->get_where('likes', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_all_likes_by_type($like_types = array('tweet', 'reply', 'album', 'poll')) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($like_types) && is_array($like_types)) {
      $query_cond = array();
      $this->db->where_in('like_type', $like_types);
      $this->db->join('users', 'user_id = like_user_id');
      $query_exec = $this->db->get_where('likes', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_unnotify_likes($like_type = NULL, $like_type_id = NULL, $user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($like_type) && !empty($like_type_id) && is_numeric($like_type_id)) {

      // Getting unnotified likes
      $query_cond = array(
          'like_type' => $like_type,
          'like_type_id' => $like_type_id,
          'like_notify_status' => 0,
          'user_status' => 1,
          'like_user_id != ' => $user_id
      );
      $this->db->join('users', 'user_id = like_user_id');
      $this->db->order_by('like_date', 'desc');
      $query_exec = $this->db->get_where('likes', $query_cond);

      // Update notified status after reterival
      $query_upda = array();
      $query_upda['like_notify_status'] = 1;
      $query_cond_2 = array();
      $query_cond_2['like_type '] = $like_type;
      $query_cond_2['like_type_id '] = $like_type_id;
      $query_exec_2 = $this->db->update('likes', $query_upda, $query_cond_2);

      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  // Remove (Unlike) all likes of a post
  function del_likes_by_post($like_type = NULL, $like_type_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($like_type) && !empty($like_type_id) && is_numeric($like_type_id)) {
      $query_cond = array(
          'like_type' => $like_type,
          'like_type_id' => $like_type_id,
      );
      $query_exec = $this->db->delete('likes', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = 1;
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

}

?>
